package com.useeinfo.asg.modules.logging.biz;

import com.useeinfo.asg.common.utils.CommUtil;
import com.useeinfo.asg.modules.logging.dao.AddressDao;
import com.useeinfo.asg.modules.logging.entity.Address;
import com.useeinfo.framework.extend.biz.CrudBiz;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * Author: */
@Service("AddressBizWeb")
public class AddressBiz extends CrudBiz<AddressDao, Address> {

    // 自定义hql
    public List<Address> queryObjects(String jpql, Map<String, Object> params) {

        return dao.findObjects(jpql, params);
    }

    // 原生查询
    public List<Object> queryListByNative(String sql) {

        return dao.findListByNative(sql);
    }


    @Transactional(readOnly = true)
    public List<Address> getAddrList(Long pId) {

        if(CommUtil.null2Long(pId) > 0) {
            return this.queryObjects("from Address o where o.deleteStatus = 0 and o.parent.id = " + CommUtil.null2Long(pId)+"order by id desc", null);
        }else {
            return this.queryObjects("from Address o where o.deleteStatus = 0 and o.level = 3 order by sequence asc", null);
        }
    }

    @Transactional(readOnly = false)
    public List<Address> getOnlyAddrList(Long id) {

        List<Address> addressList = new ArrayList<Address>();
        Address address = null;
        if(CommUtil.null2Long(id) > 0 && (address = dao.findModel(CommUtil.null2Long(id))) != null) {
            addressList.add(address);
        }
        return addressList;
    }

    @Transactional(readOnly = false)
    public JSONObject addObs(String addrName, String parentAddr, String addrCode) {

        JSONObject retJson = CommUtil.jsonResult(false);
        Address addr = new Address();
        addr.setCreateTime(new Date());
        addr.setAddrName(CommUtil.null2String(addrName));
        addr.setAddrCode(CommUtil.null2String(addrCode));
        Address parent = null;
        if(CommUtil.null2Long(parentAddr) != 0l && ((parent = dao.findModel(CommUtil.null2Long(parentAddr))) != null)) {
            addr.setLevel(4);
        }else {
            addr.setLevel(3);
        }
        addr.setParent(parent);
        if(dao.add(addr) == 1) {
            retJson = CommUtil.jsonResult(true);
        }
        return retJson;
    }

    @Transactional(readOnly = false)
    public JSONObject editObs(String id, String addrName, String parentAddr, String addrCode) {

        JSONObject retJson = CommUtil.jsonResult(false);
        Address addr = null;
        if(CommUtil.null2Long(id) > 0l && ((addr = dao.findModel(CommUtil.null2Long(id))) != null)) {

            addr.setAddrName(CommUtil.null2String(addrName));
            addr.setAddrCode(CommUtil.null2String(addrCode));
            Address parent = null;
            if(CommUtil.null2Long(parentAddr) != 0l && ((parent = dao.findModel(CommUtil.null2Long(parentAddr))) != null)) {
                addr.setLevel(4);
            }else {
                addr.setLevel(3);
            }
            addr.setParent(parent);
            if(dao.update(addr) == 1) {
                retJson = CommUtil.jsonResult(true);
            }
        }else {
            retJson.put("msg", "未找到数据");
        }
        return retJson;
    }

    @Transactional(readOnly = false)
    public JSONObject delete(String id) {

        JSONObject retJson = CommUtil.jsonResult(false);
        Address address = null;
        if(CommUtil.null2Long(id) > 0l && ((address = dao.findModel(CommUtil.null2Long(id))) != null)) {
            address.setDeleteStatus(true);
            if(dao.update(address) == 1) {
                retJson = CommUtil.jsonResult(true);
            }
        }else {
            retJson.put("msg", "未找到数据");
        }
        return retJson;
    }


}